package javatelephone;

//import java.util.Scanner;

import java.io.IOException;



/**
   A telephone that takes simulated keystrokes and voice input
   from the user and simulates spoken text.
*/
public class Telephone
{
   /**
      Construct phone object.
      @param aScanner that reads text from a character-input stream
   */
   public Telephone(InterfaceUser aInterfaceUser)
   {
        interfaceUser = aInterfaceUser;
   }
   /**
      Speak a message to System.out.
      @param output the text that will be "spoken"
   */
   public void speak(String output)
   {
      //System.out.println(output);
	   interfaceUser.ViewMessageTelephone(output);
   }

   /**
      Loops reading user input and passes the input to the
      Connection object's methods dial, record or hangup.
      @param c the connection that connects this phone to the
      voice mail system
   */
   public void run(Connection c) throws IOException
   {
      boolean more = true;
      while (more)
      {
         String input = interfaceUser.DiscardTelephone();
         
         if (input == null ) return;
         if (input.equalsIgnoreCase("H"))
            c.hangup();
         else if (input.equalsIgnoreCase("Q"))
            more = false;
         else if (input.length() == 1 && "1234567890#".indexOf(input) >= 0){
            c.dial(input);
         }
         else
         {
             if (!input.isEmpty()) 
                c.record(input);
         }
      }
   }
   private InterfaceUser interfaceUser;
}
